import { Controller, Get, Post, UseGuards, UseInterceptors, UploadedFile, Body } from '@nestjs/common'
import { Responser } from '@app/decorators/responser.decorator'
import { QueryParams, QueryParamsResult } from '@app/decorators/queryparams.decorator'
import { StatisticService, Statistic } from './expansion.service.statistic'
import { AuthUserGuard } from '@app/guards/auth-user.guard'
import { FileInterceptor } from '@nestjs/platform-express'
import * as APP_CONFIG from '@app/app.config'
import { QiNiuService } from '@app/processors/helper/helper.service.qiniu'
@Controller('expansion')
export class ExpansionController {
  constructor(
    private readonly qiniuService: QiNiuService,
    private readonly statisticService: StatisticService
  ) { }

  @Get('statistic')
  @UseGuards(AuthUserGuard)
  @Responser.handle('Get statistic')
  getSystemStatistics(@QueryParams() { isUnauthenticated }: QueryParamsResult): Promise<Statistic> {
    return this.statisticService.getStatistic(isUnauthenticated)
  }

  @Post('upload')
  @UseGuards(AuthUserGuard)
  @UseInterceptors(FileInterceptor('file'))
  @Responser.handle('Upload file to cloud storage')
  uploadStatic(@UploadedFile() file: Express.Multer.File, @Body() body) {
    return this.qiniuService
      .uploadFile({
        name: body.name,
        file: file.buffer,
        region: APP_CONFIG.QINIU.pic_bucket,
        bucket: APP_CONFIG.QINIU.pic_bucket,
      })
      .then((result) => {
        return {
          ...result,
          url: `${APP_CONFIG.APP.STATIC_URL}/${result.key}`,
        }
      })
  }
  // @Post('upload')
  // @UseGuards(AuthUserGuard)
  // @UseInterceptors(FileInterceptor('file'))
  // @Responser.handle('Upload file to cloud storage')
  // uploadStatic(@UploadedFile() file: Express.Multer.File, @Body() body) {
  //   return this.awsService
  //     .uploadFile({
  //       name: body.name,
  //       file: file.buffer,
  //       fileContentType: file.mimetype,
  //       region: APP_CONFIG.AWS.s3StaticRegion,
  //       bucket: APP_CONFIG.AWS.s3StaticBucket,
  //     })
  //     .then((result) => ({
  //       ...result,
  //       url: `${APP_CONFIG.APP.STATIC_URL}/${result.key}`,
  //     }))
  // }
}
